package com.car.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.car.entity.Citytable;
import com.car.entity.Code;
import com.car.entity.Provincialtable;
import com.car.entity.Storetable;
import com.car.entity.Vehiclebrand;
import com.car.page.Page;
import com.car.service.CartableService;
import com.car.service.CartypeService;
import com.car.service.CitytableService;
import com.car.service.CodeService;
import com.car.service.DisplacementService;

import com.car.service.GrabTheService;

import com.car.service.ProvincialtableService;
import com.car.service.StoretableService;
import com.car.service.VehiclebrandService;
import com.car.service.VehiclemotioncarService;
import com.car.vo.CartableVo;import com.car.service.GrabTheService;
import com.car.vo.GrabTheVo;
import com.car.vo.VehiclemotioncarVo;

import ch.qos.logback.classic.net.SyslogAppender;

@Controller
@RequestMapping("/jump")
public class jumpController {

	@Autowired
	GrabTheService grabTheService;
	@Autowired
	CodeService codeService;
	@Autowired
	CartableService carService;
	@Autowired
	VehiclebrandService vehService;
	@Autowired
	CartypeService cartypeService;
	@Autowired
	DisplacementService disService;
	@Autowired
	StoretableService storetableService;
	@Autowired
	CitytableService citytableService;
		@Autowired
	ProvincialtableService provincialtableService;

	@Autowired
	VehiclemotioncarService vehiclemotioncarService;

	@RequestMapping("/selectGrabtheAll")
	public String couponsShow(@RequestParam(value = "codeId", required = false) Integer codeId, Model model) {
		if (codeId == null) {
			codeId = 0;
		}
		List<GrabTheVo> list = grabTheService.selectAllByCodeId(codeId);
		List<Code> code = codeService.selectAll();
		model.addAttribute("grabList", list);
		model.addAttribute("codeList", code);
		model.addAttribute("codeId", codeId); // 参数传递
		return "/mail_detail";
	}

	@RequestMapping("/vehicle")
	public String vehicle(@RequestParam(value = "carId", required = false, defaultValue = "0") String carId,
			@RequestParam(value = "carState", required = false, defaultValue = "0") String carState, Model model,
			HttpSession session) {
		List<CartableVo> carList = carService.selByIdAndState(carId, carState);
		model.addAttribute("carList", carList);
		Map<String, Object> listInfo = new HashMap<>();
		List<Vehiclebrand> veh = vehService.selectAll();
		listInfo.put("veh", veh);
		session.setAttribute("listinfo", listInfo);
		return "/vehicleManagement";
	} 
/**
 * 顺风车查询
    * @Title: toShuncar
    * @Description: TODO(这里用一句话描述这个方法的作用)
    * @param @param takeCarCity
    * @param @param carCity
    * @param @param pageNo
    * @param @param pageSize
    * @param @param model
    * @param @param httpSession
    * @param @return    参数
    * @return String    返回类型
    * @throws
 */
	@RequestMapping("/toShunCar")
	public String toShuncar(@RequestParam(value = "takeCarCity", required = false) String takeCarCity,
						@RequestParam(value = "carCity", required = false) String carCity,
						@RequestParam(value = "startProvince", required = false) String startProvince,
						@RequestParam(value = "stopProvince", required = false) String stopProvince,
			 			@RequestParam(value = "pageIndex", required = false, defaultValue = "1") Integer pageNo,
						@RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize,
						Model model,HttpSession httpSession){
		Long beginTime = System.currentTimeMillis();
		Page<VehiclemotioncarVo> page= vehiclemotioncarService.selectAllShunCar(takeCarCity, carCity, pageNo, pageSize);
				 List<Provincialtable> proList= provincialtableService.selectAll();
			List<Storetable> storeList= storetableService.selectAll();
				 List<Vehiclebrand> veh=vehService.selectAll();
	List<Citytable> cityList = citytableService.selectAllToShow();
	 model.addAttribute("veh", veh);
		 model.addAttribute("cityList", cityList);
		model.addAttribute("page", page);
		model.addAttribute("proList", proList);
		if(startProvince!=null ||stopProvince!=null){
			List<Citytable> liststart= citytableService.selectAll(startProvince);
			List<Citytable> liststop= citytableService.selectAll(stopProvince);
			model.addAttribute("liststart",liststart);
			model.addAttribute("liststop",liststop);
		}
		if(takeCarCity==null){
			takeCarCity = "";
		}
		if(carCity==null){
			carCity = "";
		}
		model.addAttribute("startProvince", startProvince);
		model.addAttribute("stopProvince", stopProvince);
		model.addAttribute("takeCarCity", takeCarCity);
		model.addAttribute("carCity", carCity);
		model.addAttribute("storeList", storeList);
		model.addAttribute("vehiList", page.getList());
		model.addAttribute("counts", page.getCurrentPage());
		long time = System.currentTimeMillis()-beginTime;
		System.out.println("查询数据时间"+time);
		return "shunCar";
	}
	
	@RequestMapping("/trip")
	public String toTrip(@RequestParam(value = "carId", required = false) Integer carId,
 			@RequestParam(value = "pageIndex", required = false, defaultValue = "1") Integer pageNo,
			@RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize,
			Model model){
	 Page<CartableVo> pages=carService.selectAllToInfos(carId,pageNo,pageSize);
	 model.addAttribute("carlist",pages.getList());
	 model.addAttribute("pages",pages);
	 model.addAttribute("carId",carId);
		return "trip";
	}

}
